package com.zx._03_并发.chapter_04_public;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j;

/**
 * 对象发布——安全发布对象——单例模式1——懒汉模式(线程不安全的方式，不是安全发布对象的方式)
 */
@Slf4j
public class Demo_03_singleton {

    // 单例对象
    private static Demo_03_singleton singleton = null;

    // 私有构造方法
    private Demo_03_singleton() {
    }

    // 静态工厂方法
    public static Demo_03_singleton getInstance() {
        if (singleton == null) {
            try {
                Thread.sleep(1L);//模拟初始化的时候比较消耗时间
            } catch (InterruptedException e) {
            }
            singleton = new Demo_03_singleton();
        }
        return singleton;
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 20; i++) {
            pool.execute(() -> log.info(Demo_03_singleton.getInstance().toString()));
        }
        pool.shutdown();
    }
}
